package com.linko.intelligent.sys.core.controller;

import com.github.pagehelper.PageInfo;
import com.linko.intelligent.base.CommonResponse;
import com.linko.intelligent.sys.core.service.RoleService;
import com.linko.intelligent.sys.pojo.dto.RoleDto;
import com.linko.intelligent.sys.pojo.po.Role;
import com.linko.intelligent.sys.pojo.query.RoleQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@RestController
@RequestMapping("/role")
@Api(value = "RoleController", tags = "角色")
public class RoleController {

    @Autowired
    RoleService roleService;


    /***
     * 新增
     * @param dto
     * @return
     */
    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public CommonResponse save(@RequestBody RoleDto dto) {
        roleService.save(dto);
        return new CommonResponse(CommonResponse.SUCCESS, "保存成功");
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}/del")
    public CommonResponse del(@PathVariable("id") Long id) {
        roleService.delete(id);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

    @ApiOperation("修改")
    @PutMapping(value = "/{id}/update")
    public CommonResponse update(@PathVariable("id") Long id, @RequestBody RoleDto dto) {
        roleService.update(id, dto);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public CommonResponse<Role> get(@PathVariable("id") Long id) {
        Role po = roleService.getById(id);
        return new CommonResponse<Role>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("详情-关联内容")
    @GetMapping(value = "/{id}/detail")
    public CommonResponse<Role> detail(@PathVariable("id") Long id) {
        Role po = roleService.getDetail(id);
        return new CommonResponse<Role>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public CommonResponse<List<Role>> list(@SpringQueryMap RoleQuery query) {
        List<Role> list = roleService.queryList(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功", list);
    }

    /***
     * @param query
     * @return
     */
    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public CommonResponse<PageInfo<Role>> page(@SpringQueryMap RoleQuery query) {
        PageInfo<Role> pageInfo = roleService.queryPage(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功!", pageInfo);
    }

}
